package com.lzx.iteam.widget;


import android.content.Context;
import android.graphics.Point;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;

/**
 * @author zxy
 *
 */
public class ChatGroupLayout extends FrameLayout{

    Context context;
    int dis;
    Point start = new Point();
    int currentPage;
    OnChangeListener listener;
    
    public ChatGroupLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        init();
    }
    
    public void init(){
        currentPage=1;
    }
    
    
    
    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        int height = getHeight();
        int width = getWidth();
        int count = getChildCount();
        for(int i=0;i<count;i++){
            View view = getChildAt(i);
            if(view.getVisibility()!=View.GONE){
               if(i==1){
                   view.layout(0, 0, width, height);
               }else if(i==0){
                   view.measure(MeasureSpec.makeMeasureSpec(width/2, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));
                   view.layout(width/2, 0, width, height);
               }
            }
        }
    }
    
    public void setCurrentPage(int page){
        this.currentPage = page;
        if(page==0){
            ChatListGroup clg = (ChatListGroup) getChildAt(1);
            clg.setOpen(true);
        }
    }
    
    
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        System.out.println("move");
        int action = ev.getAction();
        switch(action){
            case MotionEvent.ACTION_DOWN:
                start.set((int)ev.getX(), (int)ev.getY());
                return false;
            case MotionEvent.ACTION_MOVE:
                float disx = ev.getX()-start.x;
                float disy = ev.getY()-start.y;
                System.out.println("currentpage:"+currentPage);
                if(Math.abs(disx)>16&&Math.abs(disy)/Math.abs(disx)<1){
                    
                    if(currentPage==1){
                        return true;
                    }else{
                        View view = getChildAt(0);
                        Rect rect = new Rect();
                        view.getGlobalVisibleRect(rect);
                        if(rect.contains(start.x, start.y)){
                            System.out.println("false");
                            return false;
                        }else{
                            System.out.println("true");
                            return true;
                        }
                    }
                    
                }
                break;
        }
        return false;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int action = event.getAction();
        switch(action){
            case MotionEvent.ACTION_UP:
                int scrollx =(int) (event.getX()-start.x);
                int width = getWidth();
                if(scrollx<0&&Math.abs(scrollx)>=width/3&&currentPage==1){
                    setSelect(0);
                }else if(scrollx>0&&currentPage==0){
                    setSelect(1);
                }
                break;
        }
        return true;
    }
    
    public void setListener(OnChangeListener listener){
        this.listener = listener;
    }
    
    public int getCurrentPage(){
        return currentPage;
    }
    
    public void setSelect(final int index){
        if(index>=getChildCount()||index<0) return;
        int width = getWidth();
        switch(index){
            case 0:
                if(currentPage==0) return;
                ChatListGroup clg = (ChatListGroup) getChildAt(1);
                clg.scrollToOpen();
                currentPage=0;
                if(listener!=null)
                    listener.onChange(currentPage);
               /* this.postDelayed(new Runnable() {
                    
                    @Override
                    public void run() {
                        InputMethodManager inputMgr = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
                        if(inputMgr.isActive()){
                            inputMgr.hideSoftInputFromWindow(getWindowToken(), 0);
                        }
                    }
                }, 100);*/
                break;
            case 1:
                if(currentPage==1) return;
                clg = (ChatListGroup) getChildAt(1);
                clg.scrollToHide();
                currentPage = 1;
                if(listener!=null)
                    listener.onChange(currentPage);
                break;

        }
        
    }
    
    public interface OnChangeListener{
        public void onChange(int index);
    }
    
}
